package com.yang.web.exception;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@ControllerAdvice
public class MyException {

    @ExceptionHandler(UserNotException.class)
    public String userNotExeptionHandle(Exception e, HttpServletRequest request) {
        /**
         * Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
         * 设置错误代码
         */
        request.setAttribute("javax.servlet.error.status_code", 500);
        Map<String, Object> map = new HashMap<>();
        map.put("errorCode", "user not exist");
        map.put("message", "用户出错了");
        request.setAttribute("ext", map);
        return "forward:/error";
    }
}
